package org.example.utils;

import lombok.extern.slf4j.Slf4j;
import org.example.dto.AccessTokenDTO;
import org.example.dto.WxOfficalTicket;
import org.example.dto.WxOfficalUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class WxUtil {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${wx.appid}")
    private String appid;

    @Value("${wx.secret}")
    private String secret;

    private static final Map<String, Object> map = new HashMap<>();
    private static final String QR_CODE_URL_PREFIX = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=";

    /**
     * 获取token
     *
     * @return
     */
    public AccessTokenDTO getAccessToken() {
        String url = MessageFormat.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret);

        if (map.containsKey("accessToken")) {
            return (AccessTokenDTO) map.get("accessToken");
        }
        AccessTokenDTO accessTokenDTO = restTemplate.getForObject(url, AccessTokenDTO.class);
        map.put("accessToken", accessTokenDTO);
        return accessTokenDTO;
    }

    /**
     * 获取token
     *
     * @return
     */
    public AccessTokenDTO getAccessToken(String appid, String secret) {
        String url = MessageFormat.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret);
        if (map.containsKey("accessToken")) {
            return (AccessTokenDTO) map.get("accessToken");
        }
        AccessTokenDTO accessTokenDTO = restTemplate.getForObject(url, AccessTokenDTO.class);
        map.put("accessToken", accessTokenDTO);
        return accessTokenDTO;
    }

    /**
     * 根据access_token获取Ticket
     */
    public WxOfficalTicket getTicket(String accessToken, String scene) {
        String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + accessToken;
        String body = "{\"expire_seconds\": 604800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": " + scene + "}}}";
        HttpHeaders headers = new HttpHeaders();
        //调用set方法放入请求头
        headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        HttpEntity<String> request = new HttpEntity<>(body, headers);
        return restTemplate.postForObject(url, request, WxOfficalTicket.class);
    }

    public WxOfficalUserInfo getUserInfo(String accessToken, String openId) {
        return restTemplate.getForObject("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openId + "&lang=zh_CN", WxOfficalUserInfo.class);
    }

    public String getQrCode(String ticket) {
        log.error("ticket:{}", ticket);
        return MessageFormat.format("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={0}", URI.create(ticket).toASCIIString());
    }
}
